package com.gzist.elderlyserviceplatform.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzist.elderlyserviceplatform.common.Result;
import com.gzist.elderlyserviceplatform.entity.dto.InfoNamePageDTO;
import com.gzist.elderlyserviceplatform.entity.po.Activity;
import com.gzist.elderlyserviceplatform.service.ActivityService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Optional;

/**
 * 公益活动表
 */
@Slf4j
@RestController
@Tag(name = "公益活动管理")
@RequestMapping("/api/admin/activity")
@RequiredArgsConstructor
public class ActivityControllerA {
    private final ActivityService activityService;

    @PostMapping("/add")
    @Operation(summary = "公益活动添加", description = "Activity")
    public Result<Object> addActivity(
            @RequestBody
            @Parameter(description = "公益活动信息") Activity activity
    ) {
        activityService.save(activity);
        return Result.success("操作成功！");
    }

    @PostMapping("/list")
    @Operation(summary = "公益活动列表查询", description = "模糊分页查询")
    public Result<Page<Activity>> queryActivityList(
            @RequestBody
            @Parameter(description = "查询参数") InfoNamePageDTO infoNamePageDTO
    ) {
        return Result.success(activityService.queryPage(infoNamePageDTO), "查询成功");
    }

    @PostMapping("/update")
    @Operation(summary = "公益活动修改", description = "Activity")
    public Result<Object> updateActivity(
            @RequestBody
            @Parameter(description = "修改活动的") Activity activity
    ) {
        activityService.updateById(activity);
        return Result.success("操作成功！");
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "公益活动查看详情", description = "根据id查询")
    public Result<Activity> getActivityById(
            @PathVariable
            @Parameter(description = "公益活动id") String id
    ) {
        return Result.success(activityService.getById(id), "查询成功");
    }

    @GetMapping("/getContent/{id}")
    @Operation(summary = "公益活动内容查看详情", description = "根据id查询")
    public Result<String> getActivityContentById(
            @PathVariable
            @Parameter(description = "公益活动id") String id
    ) {
        String content = Optional.ofNullable(activityService.getById(id).getContent()).orElse("");
        return Result.success(content, "查询成功");
    }

    @PostMapping("/delete/{id}")
    @Operation(summary = "公益活动删除", description = "传递公益活动信息id,根据id删除信息")
    public Result<Object> deleteActivity(
            @PathVariable
            @Parameter(description = "公益活动id") String id
    ) {
        activityService.removeById(id);
        return Result.success("操作成功！");
    }

    @PostMapping("/batchDelete")
    @Operation(summary = "公益活动批量删除", description = "传递公益活动信息id集,根据id批量删除")
    public Result<Object> batchDeleteActivity(
            @RequestBody
            @Parameter(description = "公益活动id集") String[] ids
    ) {
        activityService.removeByIds(Arrays.asList(ids));
        return Result.success("操作成功！");
    }
}